package net.longjin.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringStrUtils {

    /**
     * 将str通过分隔符进行分割,转成List
     *
     * @param str
     * @return
     */
    public static List<String> asList(String str) {
        return asList(str, ",");
    }


    /**
     * @param str 待分割字符串
     * @param lit 分割符
     * @return
     */
    public static List<String> asList(String str, String lit) {
        if (StringUtils.isNotBlank(str)) {
            List<String> list = new ArrayList<>();
            String[] split = str.split(lit);
            for (String s : split) {
                list.add(s);
            }
            return list;
        } else {
            return null;
        }
    }


    /**
     * 将小时(type-->String) 转化为 毫秒
     */
    public static Long convertToMil(String hour) {
        int i = Integer.parseInt(hour);
        BigDecimal bigDecimal = new BigDecimal(i);
        // 直接乘 可能会出现小数 超过int最大值
        bigDecimal = bigDecimal.multiply(new BigDecimal(60 * 60 * 1000));
        return bigDecimal.longValue();
    }






    /**
     * 获取uuid
     *
     * @return
     */
    public static String getUUID() {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        return uuid;
    }


    /**
     * 获取今天年月日 日期
     */
    public static String getTodayDate() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String workTime = sdf.format(new Date());
        return workTime;
    }

    /**
     * 获取昨天 年月日 日期
     */
    public static String getYesterDay() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, -1);
        Date d = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String workTime = sdf.format(d);//获取昨天日期
        return workTime;
    }


    /**
     * 获取明天 年月日 日期
     */
    public static String getTommorrow() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 1);
        Date d = cal.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String workTime = sdf.format(d);//获取昨天日期
        return workTime;
    }

    /**
     * 把一个字符串的首字母转成大写
     */
    public static String convertStartCharToUpperCase(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }
        return str.substring(0, 1).toUpperCase().concat(str.substring(1));
    }


    /**
     * 根据dictValue获取对应默认布局值
     */
    public static String getLayout(String dictValue) {
        StringBuilder sb = new StringBuilder();
        List<String> list = StringStrUtils.asList(dictValue);
        String layout = null;
        if (null != list && list.size() > 0) {
            sb.append("[");
            if (list.contains("memberWorkTitle")) {
                sb.append("{\"x\":0,\"y\":0,\"w\":24,\"h\":4.2,\"i\":\"memberWorkTitle\"},");
            }
            if (list.contains("memberCaseRemind")) {
                sb.append("{\"x\":0,\"y\":6,\"w\":14,\"h\":6.5,\"i\":\"memberCaseRemind\"},");
            }
            if (list.contains("memberCastTotal")) {
                sb.append("{\"x\":14,\"y\":6,\"w\":10,\"h\":6.5,\"i\":\"memberCastTotal\"},");
            }
            if (list.contains("todoItems")) {
                sb.append("{\"x\":0,\"y\":8,\"w\":24,\"h\":10,\"i\":\"todoItems\"},");
            }
            if (list.contains("squadronWorkTitle")) {
                sb.append("{\"x\":0,\"y\":12,\"w\":24,\"h\":3.5,\"i\":\"squadronWorkTitle\"},");
            }
            if (list.contains("squadronCaseRemind")) {
                sb.append("{\"x\":0,\"y\":16,\"w\":14,\"h\":6.5,\"i\":\"squadronCaseRemind\"},");
            }
            if (list.contains("squadronCaseFiling")) {
                sb.append("{\"x\":14,\"y\":16,\"w\":10,\"h\":6.5,\"i\":\"squadronCaseFiling\"},");
            }
            if (list.contains("squadronTodoItems")) {
                sb.append("{\"x\":0,\"y\":17,\"w\":14,\"h\":10,\"i\":\"squadronTodoItems\"},");
            }
            if (list.contains("squadronCaseTrend")) {
                sb.append("{\"x\":14,\"y\":17,\"w\":10,\"h\":10,\"i\":\"squadronCaseTrend\"},");
            }
            if (list.contains("squadronCaseNumber")) {
                sb.append("{\"x\":0,\"y\":18,\"w\":14,\"h\":10,\"i\":\"squadronCaseNumber\"},");
            }
            if (list.contains("squadronCaseAddressType")) {
                sb.append("{\"x\":14,\"y\":18,\"w\":10,\"h\":10,\"i\":\"squadronCaseAddressType\"},");
            }
            if (list.contains("squadronCaseSort")) {
                sb.append("{\"x\":0,\"y\":24,\"w\":24,\"h\":12.5,\"i\":\"squadronCaseSort\"},");
            }
            if (list.contains("squadronCaseMember")) {
                sb.append("{\"x\":0,\"y\":24,\"w\":24,\"h\":10,\"i\":\"squadronCaseMember\"},");
            }
            if (list.contains("leaderWorkTitle")) {
                sb.append("{\"x\":0,\"y\":28,\"w\":24,\"h\":3.8,\"i\":\"leaderWorkTitle\"},");
            }
            if (list.contains("leaderTodoItems")) {
                sb.append("{\"x\":0,\"y\":30,\"w\":14,\"h\":10,\"i\":\"leaderTodoItems\"},");
            }
            if (list.contains("leaderCaseTrend")) {
                sb.append("{\"x\":14,\"y\":30,\"w\":10,\"h\":10,\"i\":\"leaderCaseTrend\"},");
            }
            if (list.contains("leaderCaseAmount")) {
                sb.append("{\"x\":0,\"y\":31,\"w\":14,\"h\":10,\"i\":\"leaderCaseAmount\"},");
            }
            if (list.contains("leaderCaseTarget")) {
                sb.append("{\"x\":14,\"y\":31,\"w\":5,\"h\":10,\"i\":\"leaderCaseTarget\"},");
            }
            if (list.contains("leaderCaseFiling")) {
                sb.append("{\"x\":19,\"y\":31,\"w\":5,\"h\":10,\"i\":\"leaderCaseFiling\"},");
            }
            if (list.contains("leaderCaseNumber")) {
                sb.append("{\"x\":0,\"y\":32,\"w\":14,\"h\":10,\"i\":\"leaderCaseNumber\"},");
            }
            if (list.contains("leaderCaseAddressType")) {
                sb.append("{\"x\":14,\"y\":32,\"w\":10,\"h\":10,\"i\":\"leaderCaseAddressType\"},");
            }
            if (list.contains("leaderCaseSort")) {
                sb.append("{\"x\":0,\"y\":36,\"w\":24,\"h\":12.5,\"i\":\"leaderCaseSort\"},");
            }
            layout = sb.substring(0, sb.length() - 1);
            layout += "]";
        }
        return layout;
    }

    /**
     * 获取方法中指定注解的value值返回
     *
     * @param method               方法名
     * @param validationParamValue 注解的类名
     * @return
     */
    public static String getMethodAnnotationOne(Method method, String validationParamValue) {
        String retParam = null;
        Annotation[][] parameterAnnotations = method.getParameterAnnotations();
        for (int i = 0; i < parameterAnnotations.length; i++) {
            for (int j = 0; j < parameterAnnotations[i].length; j++) {
                String str = parameterAnnotations[i][j].toString();
                if (str.indexOf(validationParamValue) > 0) {
                    retParam = str.substring(str.indexOf("=") + 1, str.indexOf(")"));
                }
            }
        }
        return retParam;
    }

    public static boolean isValidURLAddress(String url) {
        String pattern = "([h]|[H])([t]|[T])([t]|[T])([p]|[P])([s]|[S]){0,1}://([^:/]+)(:([0-9]+))?(/\\S*)*";
        return url.matches(pattern);
    }

    /**
     * 将utf-8编码的汉字转为中文
     *
     * @param str
     * @return
     * @author zhaoqiang
     */
    public static String utf8Decoding(String str) {
        String result = str;
        try {
            result = URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }



    /**
     * 验证手机号码，11位数字，1开通，第二位数必须是3456789这些数字之一 *
     *
     * @param mobileNumber
     * @return
     */
    public static boolean checkMobileNumber(String mobileNumber) {
        boolean flag = false;
        try {
            // Pattern regex = Pattern.compile("^(((13[0-9])|(15([0-3]|[5-9]))|(18[0,5-9]))\\d{8})|(0\\d{2}-\\d{8})|(0\\d{3}-\\d{7})$");
            Pattern regex = Pattern.compile("^1[3456789]\\d{9}$");
            Matcher matcher = regex.matcher(mobileNumber);
            flag = matcher.matches();
        } catch (Exception e) {
            e.printStackTrace();
            flag = false;
        }
        return flag;
    }

    /**
     * description:
     *create time: 22:46 2020/6/11
     * @return
     * @Param: null
     */
   public   static String getHostIP()throws Exception{
        InetAddress addr = InetAddress.getLocalHost();
        String ip=addr.getHostAddress(); //获取本机ip
        return ip;

    }

    /**
     * 日期类型转换为相应格式的字符串
     *
     * @param date   字符串日期
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String dateTimeToStr(Date date, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            //当前时间
            String dateStr = sdf.format(date);
            return dateStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 判断是否是手机号码
     *
     * @param mobiles
     * @return
     */
    public static boolean isMobileNO(String mobiles){
   // Pattern p = Pattern.compile("^((13[0-9])|(17[0-9])|(19[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");Pattern regex = Pattern.compile("^1[3456789]\\d{9}$");
        Pattern p = Pattern.compile("^1[3456789]\\d{9}$");
    Matcher m = p.matcher(mobiles);
     return m.matches();
    }
}
